package com.wlf.admin.common.quartz;


import cn.hutool.extra.spring.SpringUtil;
import com.wlf.admin.system.domain.ScheduleJob;
import com.wlf.admin.system.mapper.ScheduleJobMapper;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.Trigger;

import java.util.List;

/**
 * Quartz Started
 * <p>
 * Spring 初始化时检测任务, 加载 Job 任务到内存
 *
 * @author 就眠儀式
 * @serial 2.0.0
 */
public class QuartzStarted {

    /**
     * 定时任务初始化
     */
    public static void init() {
        ScheduleJobMapper scheduleJobMapper = SpringUtil.getBean(ScheduleJobMapper.class);
        Scheduler scheduler = SpringUtil.getBean(Scheduler.class);
        List<ScheduleJob> scheduleJobList = scheduleJobMapper.selectList(null);
        for (ScheduleJob scheduleJob : scheduleJobList) {
            Trigger trigger = QuartzService.getTrigger(scheduler, scheduleJob.getId());
            if (trigger == null) {
                QuartzService.createJob(scheduler, scheduleJob);
            } else {
                QuartzService.updateJob(scheduler, scheduleJob);
            }
        }
    }
}
